package com.heukyosuk.main;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.SubMenu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.view.Window;
import android.view.WindowManager;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.Toast;

import com.heukyosuk.camera.Preview;
import com.heukyosuk.file.FileSearch;


public class MainActivity extends Activity {    
	private static final int[] ITEM_DRAWABLES = {R.drawable.camera, R.drawable.file, R.drawable.snapshot, R.drawable.setting}; 
	View viewControl;
	Preview preview;
	LayoutInflater controlInflater = null;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);	
		setContentView(R.layout.activity_main);
		
		preview = new Preview(getApplicationContext());
		FrameLayout previewFrame = (FrameLayout)findViewById(R.id.preview_frame);
		previewFrame.addView(preview);

		controlInflater = LayoutInflater.from(getBaseContext());
		viewControl = controlInflater.inflate(R.layout.control_view, null);
		LayoutParams layoutParamsControl = new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
		this.addContentView(viewControl, layoutParamsControl);
		
		ArcMenu menu = (ArcMenu) findViewById(R.id.menu);
		initArcMenu(menu, ITEM_DRAWABLES);
		
		getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
	}


	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		super.onCreateOptionsMenu(menu);
		menu.setQwertyMode(true);

		SubMenu sm = menu.addSubMenu(0, 0, 0, "서브메뉴 펼치기");
		sm.add(1, 0, 0, "메뉴1");
		sm.add(1, 1, 1, "메뉴2");

		MenuItem m = menu.add(0, 1, 1, "메뉴2");
		m.setAlphabeticShortcut('a');

		menu.add(0, 2, 2, "받은파일 목록")
		.setAlphabeticShortcut('b')
		.setIcon(R.drawable.ic_launcher);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		if (item.getItemId() == 2) {
			openFolder();
			return true;
		}
		else if (item.getItemId() >= 0 && item.getItemId() <= 3) {
			Toast.makeText(this, "You clicked on " + item.getTitle(), Toast.LENGTH_LONG).show();
			return true;
		}
		else return false;
	}

	public void openFolder() { 
		Intent intent = new Intent(MainActivity.this, FileSearch.class);
		startActivityForResult(intent, 1);
	} 
	
    private void initArcMenu(ArcMenu menu, int[] itemDrawables) {
    	int menuCount = itemDrawables.length;
        for (int i = 0; i < menuCount; i++) {
            ImageView item = new ImageView(this);
            item.setImageResource(itemDrawables[i]);

            final int position = i;
            menu.addItem(item, new OnClickListener() {
                @Override
                public void onClick(View v) {
                    // Toast.makeText(MainActivity.this, "position:" + position, Toast.LENGTH_SHORT).show();
                	if(position == 1) openFolder();
                }
            });
        }
    }
}